package de.openkanban.client.ui.adminpanel;

import com.google.gwt.user.client.ui.NamedFrame;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;

import de.openkanban.client.util.Md5Utils;
import de.openkanban.client.util.RootPanel;

/**
 * GUI Element for the SignUpForm
 */
public class SignUpForm extends DynamicForm {

	public static final String TARGET = "uploadTarget";

	private StaticTextItem message;
	private PasswordItem password;
	private PasswordItem password2;

	public SignUpForm() {
		setID("SignUpID");
		setHeight100();
		setWidth100();
		setPadding(3);
		setColWidths("60%", "40%");

		// input fields
		HeaderItem header = new HeaderItem();
		header.setDefaultValue("Sign Up Form");

		// validator
		RegExpValidator emailValidator = new RegExpValidator();
		emailValidator.setErrorMessage("Invalid email address");
		emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");

		MatchesFieldValidator validator = new MatchesFieldValidator();
		validator.setOtherField("password2");
		validator.setErrorMessage("Passwords do not match");

		// input fields
		TextItem vorname = new TextItem("vorname");
		vorname.setTitle("First Name");
		vorname.setRequired(true);

		TextItem nachname = new TextItem();
		nachname.setName("nachname");
		nachname.setTitle("Last Name");
		nachname.setRequired(true);

		TextItem namensKuerzel = new TextItem();
		namensKuerzel.setName("namensKuerzel");
		namensKuerzel.setTitle("Initials");
		namensKuerzel.setRequired(true);

		// avatarUrl
		UploadItem avatarUrl = new UploadItem();
		avatarUrl.setName("avatar");
		avatarUrl.setTitle("Avatar 40x40 px");
		avatarUrl.setTitleStyle("formTitle");
		avatarUrl.setRequired(false);

		TextItem email = new TextItem();
		email.setName("email");
		email.setTitle("Email");
		email.setValue("hammertime@stop.de");
		email.setRequired(true);
		email.setValidators(emailValidator);

		password = new PasswordItem();
		password.setName("password");
		password.setTitle("Password");
		password.setRequired(true);
		password.setValidators(validator);

		password2 = new PasswordItem();
		password2.setName("password2");
		password2.setTitle("Password check");
		password2.setRequired(true);

		message = new StaticTextItem();
		message.setValue("signup failed!");
		message.setTitle("Error:");
		message.setTextAlign(Alignment.LEFT);
		message.setColSpan(2);
		message.setVisible(false);

		nachname.setValue("Hammertime");
		vorname.setValue("Stop");
		password.setValue("hh");
		password2.setValue("hh");
		namensKuerzel.setValue("hh");

		setEncoding(Encoding.MULTIPART);
		setAction("service/user/signup");
		setTarget(TARGET);

		NamedFrame frame = new NamedFrame(TARGET);
		frame.setWidth("1");
		frame.setHeight("1");
		frame.setVisible(false);

		// add components to form
		setFields(vorname, nachname, namensKuerzel, avatarUrl, email, password, password2);

		addChild(frame);

	}

	@Override
	public void submit() {
		if (validate()) {
			RootPanel.main.getAdminPanel().getVsSignUpVStack().animateHide(AnimationEffect.SLIDE, null, 250);

			String md5Password = Md5Utils.md5(password.getValueAsString());
			password.setValue(md5Password);
			password2.setValue(md5Password);

			submitForm();
		}
	}

	public StaticTextItem getMessage() {
		return message;
	}

}
